package com.canbot.u05.activity.presidemode.newcode.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IPopItemClick;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class ItemPopWindow extends PopupWindow {
    private Context mContext;
    private final View contentView;
    private int paddingUp;
    private int paddingLeft;

    @OnClick({R.id.neditor, R.id.nrename, R.id.ndelete})
    public void viewClick(View view) {
        dismiss();
        int id = view.getId();
        switch (id) {
            case R.id.neditor:
                if (iPopItemClick != null) {
                    iPopItemClick.onEditor();
                }
                break;
            case R.id.nrename:
                if (iPopItemClick != null) {
                    iPopItemClick.onRename();
                }
                break;
            case R.id.ndelete:
                if (iPopItemClick != null) {
                    iPopItemClick.onDelete();
                }
                break;
        }
    }

    public ItemPopWindow(Context context) {
        super(context);
        mContext = context;
        contentView = View.inflate(context, R.layout.popwindow_host_list_item, null);
        ButterKnife.bind(this, contentView);
        setContentView(contentView);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable());
        paddingUp = (int) mContext.getResources().getDimension(R.dimen.x6);
        paddingLeft = (int) mContext.getResources().getDimension(R.dimen.x8);
    }

    private IPopItemClick iPopItemClick;
    private static final String TAG = "ItemPopWindow";

    public void show(View view, IPopItemClick iPopItemClick) {
        this.iPopItemClick = iPopItemClick;
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        final int windowWidth = contentView.getMeasuredWidth();

        Log.e(TAG, "show: " + windowWidth);
        showAtLocation((View) view.getParent(), Gravity.START | Gravity.TOP,
                location[0] - windowWidth / 2 + paddingLeft, location[1] + view.getHeight() / 2 + paddingUp);
    }
}
